#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/12/8 17:28
# @Author  : lizhen
# @Site    : 
# @File    : reference.py
# @Software: PyCharm
from copy import copy, deepcopy


class CustomSlice:
    def test_mutable_mutable_slice(self):
        a = [11, 22]
        b = [33, 44]
        c = [a, b]
        d = c[:]
        print(id(c), id(d))
        print(id(a), id(c[0]), id(d[0]))
        print(id(b), id(c[1]), id(d[1]))
        a.append(55)
        print(c, d)


class CustomEqual:
    def test_mutable_equal(self):
        a = [11, 22]
        b = a
        print(id(a), id(b))
        a.append(33)
        print(a, b)
        c = {'user': 'guest'}
        d = c
        print(id(c), id(d))
        c['pswd'] = 'guest'
        print(c, d)


class CustomCopy:
    def test_mutable_copy(self):
        a = [11, 22]
        b = copy(a)
        print(id(a), id(b))
        a.append(55)
        print(a, b)
        a = {'user': 'guest', 'pswd': 'guest', 'values': [22, 33]}
        b = a.copy()
        print(id(a), id(b))
        a['values'].append(44)
        print(a, b)

    def test_immutable_copy(self):
        a = (11, 22)
        b = copy(a)
        print(id(a), id(b))
        print(a, b)

    def test_mutable_mutable_copy(self):
        a = [11, 22]
        b = [33, 44]
        c = [a, b]
        d = copy(c)
        print(id(c), id(d))
        print(id(a), id(c[0]), id(d[0]))
        print(id(b), id(c[1]), id(d[1]))
        a.append(55)
        print(c, d)

    def test_immutable_mutable_copy(self):
        a = [11, 22]
        b = [33, 44]
        c = (a, b)
        d = copy(c)
        print(id(c), id(d))
        print(id(a), id(c[0]), id(d[0]))
        print(id(b), id(c[1]), id(d[1]))
        a.append(55)
        print(c, d)

    def test_mutable_immutable_mutable_copy(self):
        a = [11, 22]
        b = (a,)
        c = [b]
        d = copy(c)
        print(id(a), id(b), id(c), id(d))
        print(id(c[0]), id(d[0]))
        a.append(55)
        print(c, d)

    def test_mutable_mutable_mutable_copy(self):
        a = [11, 22]
        b = [a]
        c = [b]
        d = copy(c)
        print(id(a), id(b), id(c), id(d))
        print(id(c[0]), id(d[0]))
        a.append(55)
        print(c, d)


class CustomDeepCopy:
    def test_mutable_deepcopy(self):
        a = [11, 12]
        b = deepcopy(a)
        print(id(a), id(b))
        a.append(13)
        print(a, b)

    def test_mutable_mutable_deepcopy(self):
        a = [11, 12]
        b = [13, 14]
        c = [a, b]
        d = deepcopy(c)
        print(id(c), id(d))
        print(id(a), id(c[0]), id(d[0]))
        print(id(b), id(c[1]), id(d[1]))
        a.append(20)
        print(c, d)

    def test_immutable_mutable_deepcopy(self):
        a = [11, 22]
        b = [33, 44]
        c = (a, b)
        d = deepcopy(c)
        print(id(c), id(d))
        print(id(a), id(c[0]), id(d[0]))
        print(id(b), id(c[1]), id(d[1]))
        a.append(55)
        print(c, d)

    def test_mutable_immutable_mutable_deepcopy(self):
        a = [11, 22]
        b = (a,)
        c = [b]
        d = deepcopy(c)
        print(id(a), id(b), id(c), id(d))
        print(id(c[0]), id(d[0]))
        a.append(55)
        print(c, d)

    def test_mutable_mutable_mutable_deepcopy(self):
        a = [11, 22]
        b = [a]
        c = [b]
        d = deepcopy(c)
        print(id(a), id(b), id(c), id(d))
        print(id(c[0]), id(d[0]))
        a.append(55)
        print(c, d)


def main():
    pass
    # cs = CustomSlice()
    # cs.test_mutable_mutable_slice()
    # ce = CustomEqual()
    # ce.test_mutable_equal()
    # cc = CustomCopy()
    # cc.test_mutable_copy()
    # cc.test_immutable_copy()
    # cc.test_mutable_mutable_copy()
    # cc.test_immutable_mutable_copy()
    # cc.test_mutable_immutable_mutable_copy()
    # cc.test_mutable_mutable_mutable_copy()
    # cdc = CustomDeepCopy()
    # cdc.test_mutable_deepcopy()
    # cdc.test_mutable_mutable_deepcopy()
    # cdc.test_immutable_mutable_deepcopy()
    # cdc.test_mutable_immutable_mutable_deepcopy()
    # cdc.test_mutable_mutable_mutable_deepcopy()


if __name__ == '__main__':
    main()
